package com.jqq.single_;

public class SingleTon02 {
    public static void main(String[] args) {
        System.out.println(Cat.n1);
        System.out.println(Cat.getInstance());
        System.out.println(Cat.getInstance());
    }
}

class Cat{
    public static int n1 = 999;
    private String name;
    private static Cat cat;

    //1.将构造器私有化
    //2.定义一个static静态属性对象
    //3.提供一个public的static方法，可以返回一个cat对象
    private Cat(String name) {
        this.name = name;
    }
    public static Cat getInstance(){
        if(cat == null){//如果没有创建cat对象
            cat = new Cat("小可爱");
        }
        return cat;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                '}';
    }
}